#include "stdafx.hpp"
#include "ColligereDatabaseLayoutBase.hpp"

IMPLEMENT_ABSTRACT_CLASS ( col::DatabaseLayoutBase, wxPanel );

col::DatabaseLayoutBase::DatabaseLayoutBase 
( wxWindow *pLayoutParent ) : wxPanel ( pLayoutParent, wxID_ANY, wxDefaultPosition, 
 pLayoutParent->GetClientSize(), wxCLIP_CHILDREN | wxNO_BORDER, 
 "Colligere Database Layout Base" ) 
{ 
}

col::DatabaseLayoutBase::~DatabaseLayoutBase()
{
}

void col::DatabaseLayoutBase::PassItemToInputControls ( const DatabaseItemBase &rItem )
{
	// The index of the previosly appended list control item (if any).
	long index = -1;

	foreach ( const Column &col, rItem.GetTableDescriptor().GetColumns() )
	{
		const ControlLink &ctrl_link = col.GetCtrlLink();
		const wxString &value = rItem.GetColumnValue ( col.GetIndex() );

		switch ( ctrl_link.GetType() )
		{
		case ControlLink::Type::NormalInputField:
			{
				gui::set_ctrl_value ( XRCID ( ctrl_link.GetXrcId().c_str() ), 
					GetId(), value );
				break;
			}
		case ControlLink::Type::ListCtrl:
			{
				wxListCtrl *lc = XRCCTRL ( *this, ctrl_link.GetXrcId().c_str(), wxListCtrl );
				wxASSERT ( lc );

				// Check if the item has already been appended.
				if ( index == -1 )
				{
					index = lc->InsertItem ( lc->GetItemCount(), empty_string );
				}

				// The column index must be valid if the control link
				// points to a wxListCtrl.
				wxASSERT ( ctrl_link.GetColumnIndex() != -1 );
				lc->SetItem ( index, ctrl_link.GetColumnIndex(), rItem[col.GetIndex()] );
				break;
			}
		case ControlLink::Type::PropertyGrid:
			{
				wxPropertyGrid *pg = wxDynamicCast ( FindWindowById 
					( ctrl_link.GetId() ), wxPropertyGrid );
				wxASSERT ( pg );

				if ( wxPGProperty *p = pg->GetPropertyByName 
					( ctrl_link.GetPropName() ) )
				{
					p->SetValue ( rItem[col.GetIndex()] );
					pg->RefreshProperty(p);
					break;
				}
				wxFAIL_MSG ("Invalid property grid control link!");
			}
		default:
			{
				break;
			}
		}
	}
}

void col::DatabaseLayoutBase::ClearInputControls()
{
	const DatabaseTableDeque &tables = DatabaseTableListHandler::Get().GetAllTables();

	foreach ( const DatabaseTable &t, tables )
	{
		foreach ( const Column &c, t.GetColumns() )
		{
			const ControlLink &ctrl_link = c.GetCtrlLink();
			switch ( ctrl_link.GetType() )
			{
			case ControlLink::Type::NormalInputField:
			case ControlLink::Type::ListCtrl:
				{
					wxWindow *win = XRCCTRL ( *this, ctrl_link.GetXrcId().c_str(), wxWindow );

					// The control link must be valid if not empty.
					wxASSERT ( win );

					if ( wxTextCtrl *tc = dynamic_cast < wxTextCtrl * > ( win ) )
					{
						tc->Clear();
					}
					else if ( wxComboBox *cb = dynamic_cast < wxComboBox * > ( win ) )
					{
						cb->SetValue ( empty_string );
					}
					else if ( wxCheckBox *chb = dynamic_cast < wxCheckBox * > ( win ) )
					{
						chb->SetValue ( false );
					}
					else if ( wxSpinCtrl *sc = dynamic_cast < wxSpinCtrl * > ( win ) )
					{
						sc->SetValue ( empty_string );
					}
					else if ( wxControlWithItems *cwi = dynamic_cast 
						< wxControlWithItems * > ( win ) )
					{
						cwi->Clear();
					}
					else if ( wxListCtrl *lc = dynamic_cast < wxListCtrl * > ( win ) )
					{
						lc->DeleteAllItems();
					}
					break;
				}
			case ControlLink::Type::PropertyGrid:
				{
					wxPropertyGrid *pg = wxDynamicCast ( FindWindowById 
						( ctrl_link.GetId() ), wxPropertyGrid );
					wxASSERT ( pg );
					if ( wxPGProperty *p = pg->GetPropertyByName 
						( ctrl_link.GetPropName() ) )
					{
						p->SetValue("");
						pg->RefreshProperty(p);
					}
					break;
				}
			default:
				break;
			}
		}
	}
}